//
//  GMPracticeViewCell.m
//  Teecloud
//
//  Created by 小熊 on 2018/6/28.
//  Copyright © 2018年 小熊. All rights reserved.
//

#import "GMPracticeViewCell.h"
#import "XZPractice.h"
@interface GMPracticeViewCell ()
//图片
@property (weak, nonatomic) IBOutlet UIImageView *headImg;
//标题
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
//详细
@property (weak, nonatomic) IBOutlet UILabel *detailLabel;
//时间
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;
//异常展示的图片
@property (weak, nonatomic) IBOutlet UIImageView *erroImg;
//Title距离异常图片的边距约束
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *nameLeading;

@end
//通过
#define KPASSColor [UIColor colorWithHexString:@"#009900" andAlpha:1.0]
//未完成
#define KUNPASSColor [UIColor colorWithHexString:@"#FF0000" andAlpha:1.0]
//培训中
#define KTrainColor [UIColor colorWithHexString:@"#FF9900" andAlpha:1.0]
@implementation GMPracticeViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}
- (void)setModel:(XZPractice *)model{
    _model = model;
    [self.headImg yy_setImageWithURL:[NSURL URLWithString:model.HeadUrl] placeholder:DefealtImg];
    self.titleLabel.text = model.Title;
    self.timeLabel.text = model.ExamTime;
    
    if ([model.NormalCode integerValue] > 0) {//异常
        self.erroImg.hidden = false;
        self.nameLeading.constant = 0;
    }else{
        self.nameLeading.constant = -16.f;
        self.erroImg.hidden = true;
    }
    
    if ([model.ResType integerValue] == 1) {//练习
        if (!model.IsOver) {//练习未完成
            self.detailLabel.text = @"未完成，点击继续练习";
            self.detailLabel.textColor = KUNPASSColor;
        }else{//正确率 用时
            NSString *detaiString = [NSString stringWithFormat:@"正确 %@ 用时 %@",model.CorrectRate,model.UseTime];
            [self textAttributedString:detaiString scoreString:model.CorrectRate label:self.detailLabel];
        }
    }else if([model.ResType integerValue] == 2){//测试
        if (!model.IsOver) {//未完成
            self.detailLabel.text = @"未完成，点击继续练习";
            self.detailLabel.textColor = KUNPASSColor;
        }else{//正确率 用时
            NSString *pass ;
            if (!model.IsPass) {
                pass = @"(未通过)";
            }else{
                pass = @"(通过)";
            }
            NSString *detaiString = [NSString stringWithFormat:@"%@分%@ 用时 %@",model.TestScore,pass,model.UseTime];
            NSString *scoreString = [NSString stringWithFormat:@"%@",model.TestScore];
            [self textAttributedUnString:detaiString scoreString:scoreString label:self.detailLabel isPass:model.IsPass];
        }
    }
}
/*测试正确率*/
- (void)textAttributedString:(NSString *)string scoreString:(NSString *)scoreString label:(UILabel *)label{
    NSMutableAttributedString *originalAttributedString = [[NSMutableAttributedString alloc] initWithString:string];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
    [originalAttributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, string.length)];
    [originalAttributedString addAttribute:NSForegroundColorAttributeName value:KTrainColor range:NSMakeRange(3, scoreString.length)];
    label.attributedText = originalAttributedString;
}
/*考试未通过 通过*/
- (void)textAttributedUnString:(NSString *)string scoreString:(NSString *)scoreString label:(UILabel *)label isPass:(BOOL)isPass{
    
    NSMutableAttributedString *originalAttributedString = [[NSMutableAttributedString alloc] initWithString:string];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
    [originalAttributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, string.length)];
    
    [originalAttributedString addAttribute:NSForegroundColorAttributeName value:KTrainColor range:NSMakeRange(0, scoreString.length)];
    
        //(未通过)
        if (!isPass) {
            [originalAttributedString addAttribute:NSForegroundColorAttributeName value:KUNPASSColor range:NSMakeRange(scoreString.length+1, 5)];
    
        } else {
            [originalAttributedString addAttribute:NSForegroundColorAttributeName value:KPASSColor range:NSMakeRange(scoreString.length+1, 4)];
        }
    label.attributedText = originalAttributedString;
}
@end
